<template>
	<div style="margin-top: 20px;">
		<el-form label-width="80px" label-position="left">
			<el-form-item label="应用权限">
				<el-tree
				style="height: 500px;overflow-y: auto;width: 500px;"
					ref="tree"
					:data="routesData"
					:check-strictly="checkStrictly"
					:props="defaultProps"
					show-checkbox
					node-key="id"
					class="permission-tree"
				/>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
import Api from '@/api/pages/wuye/user.js';
export default {
	props: {
		ids: {
			default: []
		},
	},
	data() {
		return {
			checkStrictly:false,
			dialogVisible: false,
			routesData: [],
			defaultProps: {
				children: 'children',
				label: 'app_name'
			},
			checkedKeys: [],
			appidObj:{},
		};
	},
	mounted() {
		this.getAppList();
	},
	methods: {
		// 获取应用
		getAppList(xiaoquId) {
			Api.getAllAppList({
				xiaoqu_id:xiaoquId
			}).then(res => {
				if (res.state) {
					let obj = {};
					res.data.forEach(item => {
						item.id = item.app_id;
						this.appidObj[item.id] = [];
						obj[item.id] = item;
						if(item.children && item.children.length) {
							delete obj[item.id]
							item.children.forEach(item1 => {
								obj[item1.id] = item1;
								if(item1.children && item1.children.length) {
									delete obj[item1.id]
									item1.children.forEach(item2 => {
										obj[item2.id] = item2;
									})
								}
							})
						}
					});
					this.routesData = res.data;
					let routeIdList = [];
					console.log(obj)
					console.log(this.ids)
					this.ids.forEach(item => {
						if (item && obj[item]) {
							routeIdList.push({ id: item });
						}
					});
					this.$nextTick(() => {
						const routes = routeIdList;
						this.$refs.tree.setCheckedNodes(routes);
					});
				} else {
					this.$message.error(res.msg);
				}
			});
		},
		
		confirmRole() {
			this.appidObj = {};
			const checkedKeysarr = this.$refs.tree.getCheckedNodes(false,true);
			checkedKeysarr.forEach(item => {
				if(item.id != item.app_id) {
					if(this.appidObj[item.app_id]) {
						this.appidObj[item.app_id].push(item.id)
					}else{
						this.appidObj[item.app_id]= [item.id]
					}
				}
			})
			let data = [];
			for (let key in this.appidObj) {
				if(this.appidObj[key] && this.appidObj[key].length) {
					let item = {
						app_id:key,
						app_rel_id:this.appidObj[key].join()
					}
					data.push(item)
				}
			}
			// console.log('data',data)
			return data;
		},
	}
};
</script>

<style lang="scss" scoped></style>
